const { Role } = require('board-game');

const extend = require('extend');

const common = require('./common');

class UndercoverRole extends Role {
    constructor(server, room, player, team, index) {
        super(server, room, player, team, index);

        this.type = common.roles.none;
        this.word = '';
        this.status = common.status.confirming;

        this.preVote = false;
        this.vote = false;
        this.votes = 0; //被投票数
        this.dead = false;
        this.help = false;
        this.messages = []; //发言内容
    }
    setType(type, word) {
        this.type = type;
        this.word = word;
    }
    doConfirm() {
        if (!this.dead) {
            this.status = common.status.confirmed;
        }
    }
    doDeny() {
        if (!this.dead) {
            this.status = common.status.deny;
        }
    }
    doPreVote(index) {
        if (!this.dead) {
            if (this.board.stage === common.stages.vote) {
                if (this.game.groups[index].roles[0] !== this && !this.game.groups[index].roles[0].dead && (!this.board.revotes.length || this.board.revotes.includes(index))) {
                    this.preVote = index;
                }
            }
        }
    }
    doVote(index) {
        if (!this.dead) {
            index = typeof index === 'undefined' ? this.preVote : index;
            if (this.game.groups[index].roles[0] !== this && !this.game.groups[index].roles[0].dead && (!this.board.revotes.length || this.board.revotes.includes(index))) {
                this.vote = index;
            }
        }
    }
    doDescribeEnd(data) {
        if (!this.dead) {
            if (data && data.msg) {
                this.messages.push(data.msg);
            }
            this.game.board.doDescribeEnd(this);
        }
    }
    clearVote() {
        this.preVote = false;
        this.vote = false;
    }
    setVotes(votes) {
        this.votes = votes;
    }
    confirm() {
        super.confirm();
        this.clearVote();
        this.dead = false;
        this.type = common.roles.none;
        this.word = '';
        this.status = common.status.confirming;
    }
    over(win, game) {
        super.over(win, game);
        this.result = extend(true, this.result, {
            roles: Enumerable.from(game.board.groups).selectMany(group => group.roles)
                .select(role => ({
                    playerName: role.player.name,
                    win: game.board.winType === common.win[role.type],
                    word: role.word,
                    type: role.type,
                    dead: role.dead,
                })).toArray(),
        });
    }
    get data() {
        return extend(true, super.data, {
            type: this.type,
            word: this.word,
            status: this.status,
            preVote: this.preVote,
            vote: this.vote,
            votes: this.votes,
            dead: this.dead,
            help: this.help,
        })
    }
    get summary() {
        return extend(true, super.summary, {
            messages: this.messages,
        })
    }
}

module.exports = UndercoverRole;
